package calmAnnotation.model;
import java.sql.SQLException;
import java.util.Observable;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import calmAnnotation.gui.MainFrame;

import jenamuseum.annotation.*;
import sql.connection.*;


public class CalmAnnotationModel extends Observable{
private DBConnector dbConnector;
private AnnotationList currentSet;
public AnnotationListModel mesAnnotations;
public boolean isUpdating;

public CalmAnnotationModel()
{
	super();
	dbConnector = new DBConnector();
	currentSet = new AnnotationList();
	mesAnnotations = new AnnotationListModel();
}

public void getAllAnnotations() throws SQLException
{
	currentSet = dbConnector.getAllAnotation();
	if (mesAnnotations.getColumnCount()==0){
	mesAnnotations.addColumn("id");
	mesAnnotations.addColumn( "Artwork");
	mesAnnotations.addColumn( "Author");
	mesAnnotations.addColumn( "Title");
	mesAnnotations.addColumn( "Body");
	mesAnnotations.addColumn( "Date");
	mesAnnotations.addColumn("Button");
	}
	while(mesAnnotations.getRowCount()>0)
	{
		mesAnnotations.removeRow(0);
	}

	for(int k = 0; k<currentSet.size();k++)
	{
		Vector rowData = new Vector();
		rowData.add(((Annotation)currentSet.get(k)).id);
		rowData.add(((Annotation)currentSet.get(k)).artwork);
		rowData.add(((Annotation)currentSet.get(k)).author);		
		rowData.add(((Annotation)currentSet.get(k)).title);
		rowData.add(((Annotation)currentSet.get(k)).body);
		rowData.add(((Annotation)currentSet.get(k)).date);
		rowData.add(((Annotation)currentSet.get(k)).id);
		rowData.setSize(8);
		mesAnnotations.getColumnCount();
		mesAnnotations.addRow(rowData);
	}

}

public void deleteAnnotation(int id) throws SQLException
{
	isUpdating=true;

	dbConnector.deleteAnnotation(id);
	getAllAnnotations();
	isUpdating=false;

}
public void insertAnnotation(Annotation annotation) throws SQLException
{
	isUpdating=true;

	dbConnector.insertAnnotation(annotation);
	getAllAnnotations();
	isUpdating=false;

	
}

public void updateAnnotation(Annotation annotation) throws SQLException
{
	isUpdating=true;
	dbConnector.updateAnnotation(annotation);
	getAllAnnotations();
	isUpdating=false;
}

public void searchByKeyword(String keyword) throws SQLException
{
	isUpdating=true;

	currentSet = dbConnector.getAllAnotationByKeyword(keyword);
	System.out.println("Search has result : " + currentSet.size());
	if (mesAnnotations.getColumnCount()==0){
	mesAnnotations.addColumn("id");
	mesAnnotations.addColumn( "Artwork");
	mesAnnotations.addColumn( "Author");
	mesAnnotations.addColumn( "Title");
	mesAnnotations.addColumn( "Body");
	mesAnnotations.addColumn( "Date");
	mesAnnotations.addColumn("Button");
	}
	while(mesAnnotations.getRowCount()>0)
	{
		mesAnnotations.removeRow(0);
	}

	for(int k = 0; k<currentSet.size();k++)
	{
		Vector rowData = new Vector();
		rowData.add(((Annotation)currentSet.get(k)).id);
		rowData.add(((Annotation)currentSet.get(k)).artwork);
		rowData.add(((Annotation)currentSet.get(k)).author);		
		rowData.add(((Annotation)currentSet.get(k)).title);
		rowData.add(((Annotation)currentSet.get(k)).body);
		rowData.add(((Annotation)currentSet.get(k)).date);
		rowData.add(((Annotation)currentSet.get(k)).id);
		rowData.setSize(8);
		mesAnnotations.getColumnCount();
		mesAnnotations.addRow(rowData);
	}
	isUpdating=false;

}

	
}
